﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace MS.Common
{
    /// <summary>
    /// 最大化窗口
    /// </summary>
    public class CMaxBaseWindow : CSubWindowBase
    {
        /// <summary>
        /// 最大化窗口
        /// </summary>
        public CMaxBaseWindow()
            : base()
        {
        }

        /// <summary>
        /// 打开一个窗口，并且仅在新打开的窗口关闭后才返回
        /// </summary>
        /// <returns></returns>
        [SecurityCritical]
        public new bool? ShowDialog()
        {
            if (this.Owner == null && this.GetHashCode() != Application.Current.MainWindow.GetHashCode())
            {
                this.Owner = Application.Current.MainWindow;
            }
            if (this.Owner as CMaxBaseWindow != null)
            {
                return SetCBaseMaxWindow();
            }
            if (this.Owner.GetType().Name != "Ribbon2013MainWindow")
            {
                return SetDefaultWindow();
            }
            try
            {
                Border borderMainView = VisualTreeTool.GetChildObjects<Border>(this.Owner, typeof(Border), "borderMainView");
                if (borderMainView == null)
                {
                    return SetDefaultWindow();
                }

                if (this.Owner.Left < -10)
                {
                    return SetDefaultWindow();
                }

                Point tmp_point = borderMainView.TranslatePoint(new Point(0, 0), this.Owner);
                double left = 0;
                double top = 0;
                if (this.Owner.WindowState != System.Windows.WindowState.Maximized)
                {
                    left = this.Owner.Left;
                    top = this.Owner.Top;
                }
                top += tmp_point.Y;

                Double tmp_width = borderMainView.ActualWidth;
                Double tmp_height = borderMainView.ActualHeight;
                tmp_height -= 1;

                //判断是否超出屏幕
                if ((left + tmp_width) > System.Windows.SystemParameters.WorkArea.Width ||
                    (top + tmp_height) > System.Windows.SystemParameters.WorkArea.Height)
                {
                    return SetDefaultWindow();
                }

                this.SetResourceReference(Window.StyleProperty, "KsyWinStyle_MaxStyle");
                this.MinWidth = this.Width = this.MaxWidth = tmp_width;
                this.MinHeight = this.Height = this.MaxHeight = tmp_height;

                this.Left = left;
                this.Top = top;
                this.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;

                return base.ShowDialog();
            }
            catch
            {
                return SetDefaultWindow();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private bool? SetDefaultWindow()
        {
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

            //设置最大化
            this.MinWidth = this.Width = this.MaxWidth = System.Windows.SystemParameters.WorkArea.Width - 10;
            this.MinHeight = this.Height = this.MaxHeight = System.Windows.SystemParameters.WorkArea.Height - 10;

            return base.ShowDialog();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private bool? SetCBaseMaxWindow()
        {
            this.SetResourceReference(Window.StyleProperty, "KsyWinStyle_MaxStyle");
            this.MinWidth = this.Width = this.MaxWidth = this.Owner.ActualWidth;
            this.MinHeight = this.Height = this.MaxHeight = this.Owner.ActualHeight;

            this.Left = this.Owner.Left;
            this.Top = this.Owner.Top;
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;

            return base.ShowDialog();
        }
    }
}
